package com.apabi.web.controller;

import com.apabi.common.pojo.JqGridResult;
import com.apabi.common.utils.LeeJSONResult;
import com.apabi.pojo.SysIpStats;
import com.apabi.pojo.SysToken;
import com.apabi.service.SysIpStatsService;
import com.apabi.service.SysTokenService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangxh.apabi on 2017/9/22.
 */
@Controller
@RequestMapping("/sysIpStats")
public class SysIpStatsController extends BaseController {

	final static Logger log = LoggerFactory.getLogger(SysIpStatsController.class);

	@Autowired
	private SysIpStatsService sysIpStatsService;

    @Autowired
    private SysTokenService sysTokenService;

	/**
	 * 显示添加sys_ip_stats页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/createSysIpStatsPage")
	public ModelAndView createSysIpStatsPage(HttpServletRequest request) {
		log.debug("显示添加sys_ip_stats页面");
		ModelAndView mv = new ModelAndView("sysIpStats/createSysIpStatsPage");
		return mv;
	}
	
	
	/**
	 * sys_ip_stats页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/sysIpStatsList")
	public String showsysIpStatsListPage(HttpServletRequest request) {
		return "sysIpStats/sysIpStatsList";
	}


	/**
	 * 查询sys_ip_stats列表内容
	 * 
	 * @param article
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/getSysIpStatsList")
	@ResponseBody
	public JqGridResult getUserInfoList(SysIpStatsController article, Integer page, Integer pageSize) {
		if (page == null) {
			page = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		JqGridResult result = sysIpStatsService.querySysIpStatsList(null, page, pageSize);

		return result;
	}

	/**
	 * 修改或增加sys_ip_stats
	 * 
	 * @param sysIpStats
	 * @return
	 */
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public LeeJSONResult saveOrUpdate(SysIpStats sysIpStats) {

		String sysIpStatsId = sysIpStats.getId();
		if (StringUtils.isEmpty(sysIpStatsId)) {
			sysIpStatsService.saveSysIpStats(sysIpStats);
		} else {
			sysIpStatsService.updateSysIpStatsById(sysIpStats);
		}
		return LeeJSONResult.ok();
	}

	/**
	 * 查询某一个sys_ip_stats
	 * 
	 * @param sysIpStatsId
	 * @param request
	 * @return
	 */
	@RequestMapping("/sysIpStatsDetail")
	public ModelAndView sysIpStatsDetail(String sysIpStatsId, HttpServletRequest request) {

		SysIpStats sysIpStats = sysIpStatsService.querySysIpStatsById(sysIpStatsId);

		ModelAndView mv = new ModelAndView("sysIpStats/sysIpStatsDetail");
		mv.addObject("sysIpStats", sysIpStats);
		return mv;
	}

	/**
	 * 删除sys_ip_stats
	 * 
	 * @param sysIpStatsId
	 * @return
	 */
	@RequestMapping("/deleteSysIpStats")
	@ResponseBody
	public LeeJSONResult deleteSysIpStats(String sysIpStatsId) {

		if (StringUtils.isEmpty(sysIpStatsId)) {
			return LeeJSONResult.errorMsg("sys_ip_statsid为空或者不存在...");
		}
		sysIpStatsService.deleteSysIpStatsById(sysIpStatsId);
		return LeeJSONResult.ok();
	}

	/**
	 * 跳转到修改sys_ip_stats页面
	 * 
	 * @param sysIpStatsId
	 * @param request
	 * @return
	 */
	@RequestMapping("/modifySysIpStats")
	public ModelAndView modifySysIpStats(String sysIpStatsId, HttpServletRequest request) {

		SysIpStats sysIpStats = sysIpStatsService.querySysIpStatsById(sysIpStatsId);

		ModelAndView mv = new ModelAndView("sysIpStats/modifySysIpStats");
		mv.addObject("sysIpStats", sysIpStats);

		return mv;
	}

	/**
	 * echarts页面
	 * @return
	 */
	@RequestMapping("/sysIpStatsEcharts")
	public ModelAndView sysIpStatsEcharts() {
	    List<SysToken> list = sysTokenService.queryAllSysTokenList();
		ModelAndView mv = new ModelAndView("sysIpStats/sysIpStatsEcharts");
		mv.addObject("SysToken", list);
		return mv;
	}

	/**
	 * echarts数据
	 * @param date
	 * @param token
	 * @return
	 */
	@RequestMapping("/echartsData")
	@ResponseBody
	public List<Map<String, Object>> echartsData(String date,String token){
		List<Map<String, Object>> list = sysIpStatsService.querySysIpStatsByDateAndtoken(date,token);
		return list;
	}
}
